﻿// 
//    Copyright 2010 The JaernCloud Project
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JaernCloud.Language.Compiler.Ast;

namespace JaernCloud.Language.Compiler.Types.Builtin.Model {
    
    /// <summary>
    /// Model is the node implementation for "model" nodes, eg. "model#task"
    /// </summary>
    public class ModelNodeType: NodeType, IModelNodeType<INodeType> {

        /// <summary>
        /// We default to persisted, but in case no persistence configuration can be found, we'll default to prototype or conversation depending on the page.
        /// </summary>
        private NodeTypeScope _scope = NodeTypeScope.Persisted;

        private string _scopeNodeId;

        public NodeTypeScope Scope {
            get { return _scope; }
            protected set { _scope = value; }
        }

        public string ScopeNodeId {
            get {
                if (_scopeNodeId == null) {
                    StringBuilder sb = new StringBuilder();
                    List<INodeType> types = new List<INodeType>();
                    INodeType current = this;
                    while (current != null) {
                        types.Add(current);
                        var parent = current.Parent;
                        if (parent is IScopedNodeType) {
                            // got to a parent that has a scopeNodeId - let it do the rest
                            sb.Append((parent as IScopedNodeType).ScopeNodeId);
                            break;
                        }
                        current = current.Parent;
                    }
                    types.Reverse();
                    foreach (INodeType type in types) {
                        sb.Append("/");
                        if (type == this) {
                            sb.Append(SourceNode.Tag).Append('@').Append(Scope);
                        } else {
                            if (type.SourceNode.Tag != null) {
                                sb.Append(type.SourceNode.Tag);
                            }
                        }
                    }
                    _scopeNodeId = sb.ToString();
                }
                return _scopeNodeId;
            }
        }

        public override AstTag GetRequiredAstTag() {
            return AstTag.FromName("model");
        }



        public override INodeType GetNodeTypeInstanceFor(AstNode node) {
            return new ModelNodeType() { SourceNode = node };
        }
    }

}
